---
--类名: Wheel.lua
--创建日期: 2017-7-20
--创建人: 陈小虎
--功能描述: 
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--

local Wheel = class("Wheel")

function Wheel:ctor(uiRoot)
    cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

   	self._uiRoot = uiRoot

   	self:init()
end

function Wheel:getUIRoot()
    return self._uiRoot
end

function Wheel:init()
    self._radiusNode = self._uiRoot:getChildByName("radius")
	self._center = self._radiusNode:getChildByName("bg")
    self._btnLeft = self._radiusNode:getChildByName("btn_l")
    self._btnRight = self._radiusNode:getChildByName("btn_r")
	self._originalCenterPosition = cc.p(self._center:getPosition())
	self._uiRoot:setCascadeColorEnabled(true)
	self._uiRoot:setTouchEnabled(true)

	self._uiRoot:addTouchEventListener(function(sender, eventType)
		if eventType == ccui.TouchEventType.began then
            self:setTouchBegan()
            local point = self._radiusNode:convertToNodeSpace(sender:getTouchBeganPosition())
            self:directionControl(point)
		elseif eventType == ccui.TouchEventType.moved then
			local point = self._radiusNode:convertToNodeSpace(sender:getTouchMovePosition())
			self:directionControl(point)
		elseif eventType == ccui.TouchEventType.ended
			or eventType == ccui.TouchEventType.canceled then
            self:setTouchEnd()
            self._btnLeft:setScale(1)
            self._btnRight:setScale(1)
			self:callControlCallback(Enums.MoveFlag.stop)
		end
	end)

    local platform = cc.Application:getInstance():getTargetPlatform()
    if platform == cc.PLATFORM_OS_WINDOWS or platform == cc.PLATFORM_OS_MAC then
        local function refreshKeyPressed()
            if self._leftkeypressed then
                self:directionControl(cc.p(-9999,0))
            elseif self._rightkeypressed then
                self:directionControl(cc.p(9999,0))
            else
                self._btnLeft:setScale(1)
                self._btnRight:setScale(1)
                self:callControlCallback(Enums.MoveFlag.stop)
            end
        end

        local function keyboardPressed(keyCode, event)
            if keyCode == 124 then
                self._leftkeypressed = true
                self:directionControl(cc.p(-9999,0))
                self:setTouchBegan()
            elseif keyCode == 127 then
                self._rightkeypressed = true
                self:directionControl(cc.p(9999,0))
                self:setTouchBegan()
            elseif keyCode == 146 or keyCode == 89 then --w + s -
                MapManager:mapTouchZoomIn(0.03,1)
            elseif keyCode == 142 or keyCode == 73 then
                MapManager:mapTouchZoomOut(0.03,1)
            end
        end

        local function keyboardReleased(keyCode, event)
            if keyCode == 124 then
                self._leftkeypressed = false
                refreshKeyPressed()
                self:setTouchEnd()
            elseif keyCode == 127 then
                self._rightkeypressed = false
                refreshKeyPressed()
                self:setTouchEnd()
            end
        end
        local listener = cc.EventListenerKeyboard:create()
        listener:registerScriptHandler(keyboardPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)
        listener:registerScriptHandler(keyboardReleased, cc.Handler.EVENT_KEYBOARD_RELEASED)
        local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
        eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self._uiRoot)
    end

    self._firstClick = false
    self._fadeVisible = true
    self._isTouchEnd = false
    self._lastTickTime = app:getTime()
    local function _tick()
        local currTime = app:getTime()
        if (currTime - self._lastTickTime > Constant.Wheel_FadeOut_Tick)
                and self._firstClick
                and self._fadeVisible
                and self._isTouchEnd
                and (not BattleManager:isBattle()) then
            self._lastTickTime = app:getTime()
            self:setFadeOut()
        end
    end

    self._fadeScheduler = scheduler.newCron(_tick, 1, true)
end

function Wheel:directionControl(pos)
--    local radius = self._radiusNode:getContentSize().width / 2
    local x = pos.x - self._originalCenterPosition.x
--    local y = pos.y - self._originalCenterPosition.y

--    local radius2 = radius * radius
--
--    if radius2 < x * x + y * y then
--        if x == 0 then
--            if y < -radius then
--                y = -radius
--            elseif y > radius then
--                y = radius
--            end
--        else
--            local temp = y / x
--            local endX = math.sqrt((radius2) / (1.0 + math.abs(temp)))
--            if x < 0 then
--                endX = -endX
--            end
--
--            local endY = math.sqrt(radius2- endX * endX)
--            if y < 0 then
--                endY = -endY
--            end
--            x = endX
--            y = endY
--        end
--    end

   	local moveFlag
    if x > 0 then
       	moveFlag = Enums.MoveFlag.right
        self._btnRight:setScale(1.1)
    else
        moveFlag = Enums.MoveFlag.left
        self._btnLeft:setScale(1.1)
    end

    self:callControlCallback(moveFlag)
end

function Wheel:setControlCallback(callback)
	self._controlCallback = callback
end

function Wheel:callControlCallback(moveFlag)
	if self._controlCallback then
		self._controlCallback(moveFlag)
	end
end

function Wheel:setTouchBegan()
    self._firstClick = true
    self._isTouchEnd = false
    if not self._fadeVisible then
        self:setFadeIn()
    end
end

function Wheel:setTouchEnd()
    self._isTouchEnd = true
    self._lastTickTime = app:getTime()
end

function Wheel:setFadeIn()
    local action = cc.Sequence:create(cc.FadeTo:create(0.5, 255))
    if action then
        self._radiusNode:runAction(action)
        self._fadeVisible = true
    end
end

function Wheel:setFadeOut()
    local action = cc.Sequence:create(cc.FadeTo:create(0.5, 255 * 0.3))
    if action then
        self._radiusNode:runAction(action)
        self._fadeVisible = false
    end
end

function Wheel:resetStatus()
    self._firstClick =  false
    self:setFadeIn()
end

-- 计时器销毁
function Wheel:disposeScheduler()
    if self._fadeScheduler then
        scheduler.cancelCron(self._fadeScheduler)
        self._fadeScheduler = nil
    end
end

return Wheel
